package com.ziyun.pop.mall.module.user.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.service.ucenter.api.UserWalletService;
import com.service.ucenter.constant.UcenterResult;
import com.service.ucenter.model.wallet.UserWalletVO;
import com.service.utils.push.umeng.JsonResult;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;

/**
 * 用户钱包
 * 
 * @author lv.ai
 * @date 2018年9月6日 下午2:07:23
 */
@Controller
public class UserWalletController {
	private static Logger _log = LoggerFactory.getLogger(UserWalletController.class);
	private static final String MODEL = "/user/userWallet";

	@Autowired
	private UserWalletService userWalletService;
	
	@Autowired
	private HttpRequestLocal httpRequestLocal;

	/**
	 * 获取用户钱包信息
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/getUserWallet.json")
	@ResponseBody
	public JsonResult<Map<String, Object>> getUserWallet() {
		Map<String, Object> map = new HashMap<String, Object>();
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		UserWalletVO userWallet = userWalletService.getUserWallet(userId).getData();
		map.put("userWallet", userWallet);
		return JsonResult.success(map);
	}

	/**
	 * 去用户中心跳转页面
	 * 
	 * @return
	 */
	@GetMapping(MODEL + "/jumpSecurity.do")
	@ResponseBody
	public ModelAndView jumpSecurity() {
		ModelAndView view = new ModelAndView("/user/userWallet/jumpSecurity.html");
		return view;
	}

	/**
	 * 验证是否设置 预存款（支付）密码
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/isSetPass.json")
	@ResponseBody
	public JsonResult<Map<String, Object>> isSetPass() {
		Map<String, Object> map = new HashMap<String, Object>();
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		UcenterResult<Boolean> result = userWalletService.isSetPass(userId);
		map.put("result", result);
		return JsonResult.success(map);
	}

	/**
	 * 校验预存款（支付）密码
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/checkAdvancePass.json")
	@ResponseBody
	public JsonResult<Map<String, Object>> checkAdvancePass(HttpServletRequest request, HttpServletResponse response) {
		String payPassword = request.getParameter("payPassword");
		Map<String, Object> map = new HashMap<String, Object>();
		UcenterResult<Boolean> result = userWalletService.checkAdvancePass(payPassword);
		map.put("result", result);
		return JsonResult.success(map);
	}

}
